<template>
	<view class="u-wrap">
		<view class="tabs">
			<!-- <view class="back" @click="backs">
				<img src="../../static/fish/back.png" alt="" />
			</view> -->
			<u-popup :height="300" :show="show" @close="close" mode="center" round="30"
				customStyle="position: relative;width: 614rpx;height:750rpx">
				<view class="cnt_titles">
					<view class="yjs">
						请编辑您的文案
					</view>
					<view class="cnt_cents">
						<u--textarea class="cent_writ" placeholder="请输入内容" :value="writing" border="none"
							@input="change" color="#332f2f" adjustPosition="true" height="400rpx"
							customStyle="background:#f3f3f3; text-align: left;font-size: 28rpx;font-weight: 400;color:#332F2F;font-family: PingFang SC, PingFang SC;"></u--textarea>
					</view>
					<view class="cnt_btomss">
						<view class="cnt_btomss_l" @click="closes">取消</view>
						<view class="cnt_btomss_r" @click="confirm">保存</view>
					</view>
				</view>
			</u-popup>
		</view>
		<!-- 内容 -->
		<view class="cnt">
			<view class="cnt_title">
				<view class="yj">
					原句
				</view>
				<view class="cnt_cent">
					{{contents}}
				</view>
				<view class="line"></view>
				<view class="cnt_btom">
					<view class="btom_l" @click="Unsmart"><img src="/static/home/rg.png" alt="">手动改写</view>
					<view class="btom_line"></view>
					<view class="btom_r" @click="smart"><img src="/static/home/AIrg.png" alt="">智能改写</view>
				</view>
			</view>
			<view class="cnt_title" v-for="(item,index) in smartcontents" :key="index">
				<view class="yj">
					改写结果
				</view>
				<view class="cnt_cent">
					{{item}}
				</view>
				<view class="cnt_btoms" @click="goRewrite(item)">
					<view class="submit" >确认使用</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		wycUrl,
		apiMixUrlApi
	} from '@/api/assemb.js'
	import {
		type
	} from '../../uni_modules/uni-forms/components/uni-forms/utils'
	export default {
		data() {
			return {
				text: 0,
				show: false,
				contents: '',
				smartcontents: [],
				content: [],
				value: [],
				title: '',
				writing: '',
				index: '',
				conts: [],
				values: [],
			}
		},
		onLoad(options) {
			let contents = JSON.parse(options.param)
			this.value = contents
			// console.log("🚀 ~ file: Gorewrite.vue:76 ~ onLoad ~ this.value:", this.value)
			this.index = contents.index
			this.contents = contents.content
			this.title = contents.title
		},
		onShow() {
			console.log('000000000000')
		},
		computed: {},
		methods: {
			// setWife(){

			// },
			featcath() {
				let param = {
					content: this.contents,
					intensity: 2,
					changeType: 1,
				}
				wycUrl(param, 1008, 'pseudoMaxAzure').then(async (res) => {
					uni.hideLoading()
					let content = res.data.data.results[0]
					this.smartcontents.push(content)
				}).catch((err) => {
					uni.hideLoading()
					console.log('🚀 ~ file: copute.vue:399 ~ wycUrl ~ err:', err)
				})
			},
			smart() {
				if (uni.getStorageSync('frequency') > 0) {
					uni.showLoading({
						title: '改写中',
					})
					let param = {
						content: this.contents,
						intensity: 2,
						changeType: 1,
					}
					wycUrl(param, 1008, 'pseudoMaxAzure').then(async (res) => {
						uni.hideLoading()
						let content = res.data.data.results[0]
						this.smartcontents.push(content)
						let params = {
							consumecoins: 1,
							descinfo: '伪原创消耗',
							consumetype: 1,
						}
						apiMixUrlApi(params, '1043', "ConsumeCoins").then(result => {
							console.log(result, '1043返回值');
							uni.setStorageSync('frequency', result.coins)
						});
					}).catch((err) => {
						uni.hideLoading()
						console.log('🚀 ~ file: copute.vue:399 ~ wycUrl ~ err:', err)
					})

				} else {
					uni.showLoading({
						title: '改写中',
					})
					this.featcath()
				}

			},

			Unsmart() {
				this.show = true;
				this.writing = this.contents
			},
			backs() {
				if (this.text == 0) {
					uni.navigateBack({
						delta: 1,
					})
				} else {
					let par = {
						content: this.value,
						values: this.conts,
						index: this.index,
						name: "back",
						title: this.title,
						select: '999'
					};
					uni.navigateTo({
						url: '/pages/aitool/rewrite?param=' + JSON.stringify(par)
					})
				}
				// uni.navigateBack({
				//   delta: 1,
				// })
				// uni.navigateTo({
				// 	url: '/pages/aitool/rewrite'
				// })
				// this.goRewrite()
				// uni.switchTab({
				//   url: '/pages/tools/toolr'
				// })
				// uni.navigateBack({
				//   delta: 1,
				// })
			},
			close() {
				this.show = false;
			},
			change(e) {
				this.writing = e
			},
			closes() {
				this.show = false;
			},
			confirm() {
				if (this.smartcontents == []) {
					uni.showToast({
						title: '文案不能为空！',
						duration: 2000
					});
					return
				}
				if (this.writing == '') {
					uni.showToast({
						title: '文案不能为空！',
						duration: 2000
					});
					return
				}
				this.show = false;
				this.smartcontents.push(this.writing)
			},
			goRewrite(e) {
				this.text = 1
				let that = this
				if (e == '') {
					uni.showToast({
						title: '文案不能为空！',
						duration: 2000
					});
					return
				}
				if (!this.title) {
					console.log('走到第1个', that.index)
					let params = {
						content: e,
						values: this.value,
						index: that.index,
						name: "back",
						select: '999'
					};
					console.log(params.values.value.content)
					// console.log(params.values.value.content,'第一个内容打印')
					for (var i = 0; i < params.values.value.content.split(',').length; i++) {
						// let index = that.index
						this.cont = params.values.value.content.split(',')[i]
						// console.log(this.cont,'第一个打印')
						this.cont = this.cont.replace('[', '').replace(']', '')
						this.cont = JSON.parse(this.cont.replace(/'/g, '"'))
						this.conts.push(this.cont)
					}
					this.conts[that.index].content = e
					this.conts[that.index]['backIndex'] = 999;
					let par = {
						content: this.value,
						values: this.conts,
						index: 4,
						name: "back",
						select: '999'
					};
					uni.redirectTo({
							url: '/pages/aitool/rewrite?param=' + JSON.stringify(par)
					})
					// uni.navigateTo({
					// 	url: '/pages/aitool/rewrite?param=' + JSON.stringify(par)
					// })
					// console.log("🚀 ~ file: Gorewrite.vue:163 ~ goRewrite ~ !this.title:", par)
				} else {
					console.log('走到第二个')
					let par = {
						content: this.value,
						values: this.conts,
						index: this.index,
						name: "back",
						title: this.title,
						select: '999'
					};
					par.content.value[this.index].content = e
					par.content.value[this.index]['backIndex'] = 999;
					uni.redirectTo({
							url: '/pages/aitool/rewrite?param=' + JSON.stringify(par)
					})
					// console.log("🚀 ~ file: Gorewrite.vue:149 ~ goRewrite ~ par:", par)
					// uni.navigateTo({
					// 	url: '/pages/aitool/rewrite?param=' + JSON.stringify(par)
					// })
				}
			},
		},
	}
</script>

<style lang="scss" scoped>
	.u-wrap {
		height: calc(100vh);
		/* #ifdef H5 */
		height: calc(100vh - var(--window-top));
		/* #endif */
		display: flex;
		flex-direction: column;
		background: #f5f5f5;
	}

	.banner-img {
		display: flex;
		justify-content: center;
		background: linear-gradient(180deg, #ffeeee 0%, #f8f8f8 100%);
	}

	.tabs {
		// background: linear-gradient(180deg, #ffeeee 0%, #f8f8f8 100%);
		width: 100%;
		height: 20rpx;
	}

	.tabbs {
		//   margin: 30rpx 0 0 41%;
		margin-left: 30%;
		margin-top: 50rpx;
	}

	.back {
		width: 48rpx;
		height: 48rpx;
		margin-left: 30rpx;
		margin-top: 50rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.cnt {
		margin: 30rpx;
	}

	.cnt_title {
		width: 690rpx;
		height: 428rpx;
		background: #ffffff;
		box-shadow: 0rpx 6rpx 12rpx 2rpx rgba(175, 175, 175, 0.16);
		border-radius: 16rpx;
		opacity: 1;
		margin-bottom: 22rpx;
	}

	.cnt_btom {
		text-indent: 2em;
		display: flex;
		align-items: center;
		justify-content: space-evenly;
		margin-top: 28rpx;

		image {
			width: 29rpx;
			height: 29rpx;
			margin-right: 10rpx;
		}
	}

	.yj {
		width: 675rpx;
		height: 62rpx;
		background: linear-gradient(90deg, #fff6f6 0%, #fffbee 100%);
		border-radius: 16rpx 16rpx 0rpx 0rpx;
		opacity: 1;
		line-height: 62rpx;
		padding-left: 15rpx;
	}

	.cnt_cent {
		width: 620rpx;
		height: 216rpx;
		text-indent: 2em;
		font-size: 28rpx;
		font-family: PingFang SC, PingFang SC;
		font-weight: 400;
		color: #332f2f;
		margin: 16rpx 36rpx;
		overflow: scroll;
	}

	.line {
		width: 691rpx;
		height: 1rpx;
		background-color: #f3f3f3;
	}

	.btom_l {
		height: 44rpx;
		font-size: 32rpx;
		font-family: PingFang SC, PingFang SC;
		font-weight: 400;
		color: #f1445d;
	}

	.btom_r {
		height: 44rpx;
		font-size: 32rpx;
		font-family: PingFang SC, PingFang SC;
		font-weight: 400;
		color: #f1445d;
	}

	.btom_line {
		width: 1rpx;
		height: 54rpx;
		background: #f3f3f3;
	}

	.cnt_btoms {
		width: 690rpx;
		height: 76rpx;
		background: #fff3ef;
		border-radius: 0rpx 0rpx 16rpx 16rpx;
		opacity: 0.89;
		margin-top: 63rpx;
	}

	.submit {
		line-height: 76rpx;
		position: absolute;
		left: 42%;
		font-size: 32rpx;
		font-family: PingFang SC, PingFang SC;
		font-weight: bold;
		color: #f1445d;
	}

	.context-textarea-input {
		width: 100%;
		height: 100%;
		box-sizing: border-box;
		padding: 22rpx 30rpx 0 30rpx;
		color: #000000;
		font-size: 28rpx;
	}

	.cnt_titles {
		width: 538rpx;
		font-size: 32rpx;
		font-family: PingFang SC, PingFang SC;
		color: #332f2f;
		font-weight: bold;
		margin-left: 40rpx;
	}

	.cnt_cents {
		height: 444rpx;
		background: #f3f3f3;
		border-radius: 40rpx;
		opacity: 1;
		padding: 30rpx;
	}

	.yjs {
		margin: 38rpx 0 14rpx 0;
	}

	.cent_writ {
		font-size: 28rpx;
		font-family: PingFang SC, PingFang SC;
		font-weight: 400;
		color: #332f2f;
		text-align: left;
	}

	.cnt_btomss {
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-top: 25rpx;
	}

	.cnt_btomss_l {
		width: 260rpx;
		height: 72rpx;
		background: #f2f2f2;
		border-radius: 38rpx;
		line-height: 72rpx;
		color: #828282;
		opacity: 1;
		display: flex;
		    justify-content: center;
	}

	.cnt_btomss_r {
		width: 260rpx;
		height: 72rpx;
		background: #f1445d;
		border-radius: 38rpx;
		color: #ffffff;
		line-height: 72rpx;
		opacity: 1;
		display: flex;
		    justify-content: center;
	}

	.u-textarea {
		background-color: #f3f3f3;
	}
</style>